<?php
/**
  * AttachmentController.php
  *
  * This is not a freeware, use is subject to license terms
  * @author CLOUD DEV TEAM
  * 
  */
if (!defined('IN_CLOUD')) exit ('Access Denied!');

class AttachmentController extends Controller
{
	public function __construct()
	{
		parent::__construct();
	}
	
	public function run()
	{
		if ( $this->post['dosubmit'] )
		{
			
		}
		
		$this->viewModel->tplAssign();

		$this->viewModel->display( 'uploadPhoto.htm' );
	}
	
	public function uploadAttachment()
	{
		if ( $this->post['user_id'] < 1 )
		{
			exit();
		}
		$dir = $this->post['dir'];
		if ( !in_array( $dir , array( 'House' , 'Estate') ) )
		{
			exit();
		}
		$authKey = Common::getConfig( 'System' , 'auth_key' );
		if ( $this->post['swfUploadKey'] != md5( $this->post['SWFUPLOADSESSID'] . $authKey ) )
		{
			exit('A');
		}
		if ( $_FILES['Filedata'] && $_FILES['Filedata']['error'] == 0 )
		{
			try {
				$upload = new Upload();
				$image = new Image();
				if ( $upload->uploadFile( $_FILES['Filedata'] , $dir ) )
				{
					$imagePath = $upload->attach['relative_path'];
					//$thumbImage = $image->small_img( $upload->attach['attach_path'] , 80 , 60 );
					$image->watermark( ROOT_DIR . ATTACHMENT . $imagePath );
					if ( $this->post['module'] == 3 )
					{
						if ( $this->post['type'] == 1 )
						{
							$width = 176;
							$height = 133;
						}
						else
						{
							$width = 147;
							$height = 110;
						}
						$thumbImage = $image->smallImg( $upload->attach['attach_path'] , $width , $height );
						$image->watermark( ROOT_DIR . ATTACHMENT . $thumbImage );
					}
					//$image->watermark( ROOT_DIR . ATTACHMENT . $thumbImage );

					$attachmentModel = Attachment::getInstance();
					$data = array(
						'module' => ( int )$this->post['module'],
						'type' => ( int )$this->post['type'],
						'pid' => 0,
						'filename' => $upload->attach['filename'],
						'filepath' => $imagePath,
						'filesize' => filesize( $upload->attach['attach_path'] ),
						'user_id' => ( int )$this->post['user_id'],
						'status' => 0,
						'pub_time' => SYSTIME,
					);
					if ( ( $aId = $attachmentModel->insert( $data , true ) ) > 0 )
					{
						echo $aId . ',' . ATTACHMENTURL . $imagePath;
					}
					else 
					{
						exit('B');
					}
				}
			} catch (Exception $e) {
				exit('C' . $e->getCode());
			}
		}
		else
		{
			exit('D');
		}
	}
	
	/**
	 * 删除附件
	 */
	public function deleteAttachment()
	{
		if ( $this->userInfo['userId'] < 1 )
		{
			return;
		}
		if ( $this->post['image_del'] )
		{
			$delImageIds = explode( '|' , $this->post['image_del'] );
			if ( $delImageIds )
			{
				$attachmentModel = Attachment::getInstance();
				foreach( $delImageIds as $aId )
				{
					if ( $aId )
					{
						$attachment = $attachmentModel->getOne( array( 'aid' => $aId , 'user_id' => $this->userInfo['userId'] , 'status' => 0 ) );
						if ( $attachment )
						{
							$thumbImage = dirname( $attachment['filepath'] ) . '/' . 'thumb_' . basename( $attachment['filepath'] );
							if ( $attachmentModel->delete( array( 'aid' => $aId ) ) )
							{
								file_exists( ROOT_DIR . ATTACHMENT . $attachment['filepath'] ) ? @unlink( ROOT_DIR . ATTACHMENT . $attachment['filepath'] ) : '';
								file_exists( ROOT_DIR . ATTACHMENT . $thumbImage ) ? @unlink( ROOT_DIR . ATTACHMENT . $thumbImage ) : '';
							}
						}
					}
				}
				echo 'success';
			}
		}
	}
}